/*   1:    */ package com.sie.driver.ftusbdriver.ft4222;
/*   2:    */ 
/*   3:    */ import com.sie.driver.ftusbdriver.FT_Device;
import com.sie.driver.ftusbdriver.interfaces.I2cSlave;
/*   5:    */ 
/*   6:    */ public class FT_4222_I2c_Slave
/*   7:    */   implements I2cSlave
/*   8:    */ {
/*   9:    */   FT_4222_Device mFt4222Dev;
/*  10:    */   FT_Device mFtDev;
/*  11:    */   
/*  12:    */   public FT_4222_I2c_Slave(FT_4222_Device ft4222Device)
/*  13:    */   {
/*  14: 14 */     this.mFt4222Dev = ft4222Device;
/*  15: 15 */     this.mFtDev = this.mFt4222Dev.mFtDev;
/*  16:    */   }
/*  17:    */   
/*  18:    */   public int cmdSet(int wValue1, int wValue2)
/*  19:    */   {
/*  20: 19 */     return this.mFtDev.VendorCmdSet(33, wValue1 | wValue2 << 8);
/*  21:    */   }
/*  22:    */   
/*  23:    */   public int cmdSet(int wValue1, int wValue2, byte[] buf, int datalen)
/*  24:    */   {
/*  25: 23 */     return this.mFtDev.VendorCmdSet(33, wValue1 | wValue2 << 8, buf, datalen);
/*  26:    */   }
/*  27:    */   
/*  28:    */   public int cmdGet(int wValue1, int wValue2, byte[] buf, int datalen)
/*  29:    */   {
/*  30: 27 */     return this.mFtDev.VendorCmdGet(32, wValue1 | wValue2 << 8, buf, datalen);
/*  31:    */   }
/*  32:    */   
/*  33:    */   public int init()
/*  34:    */   {
/*  35: 35 */     int ftStatus = this.mFt4222Dev.init();
/*  36: 36 */     if (ftStatus != 0) {
/*  37: 37 */       return ftStatus;
/*  38:    */     }
/*  39: 40 */     if (!I2C_ModeCheck()) {
/*  40: 41 */       return 1012;
/*  41:    */     }
/*  42: 50 */     ftStatus = cmdSet(5, 2);
/*  43: 51 */     if (ftStatus < 0) {
/*  44: 52 */       return ftStatus;
/*  45:    */     }
/*  46: 54 */     this.mFt4222Dev.mChipStatus.function = 2;
/*  47:    */     
/*  48: 56 */     return 0;
/*  49:    */   }
/*  50:    */   
/*  51:    */   public int reset()
/*  52:    */   {
/*  53: 63 */     byte reset = 1;
/*  54:    */     
/*  55: 65 */     int ftStatus = I2C_Check(false);
/*  56: 66 */     if (ftStatus != 0) {
/*  57: 67 */       return ftStatus;
/*  58:    */     }
/*  59: 70 */     return cmdSet(91, reset);
/*  60:    */   }
/*  61:    */   
/*  62:    */   public int getAddress(int[] addr)
/*  63:    */   {
/*  64: 76 */     byte[] bAddr = new byte[1];
/*  65:    */     
/*  66: 78 */     int ftStatus = I2C_Check(false);
/*  67: 79 */     if (ftStatus != 0) {
/*  68: 80 */       return ftStatus;
/*  69:    */     }
/*  70: 83 */     ftStatus = this.mFtDev.VendorCmdGet(33, 92, bAddr, 1);
/*  71: 85 */     if (ftStatus < 0) {
/*  72: 86 */       return 18;
/*  73:    */     }
/*  74: 88 */     addr[0] = bAddr[0];
/*  75: 89 */     return 0;
/*  76:    */   }
/*  77:    */   
/*  78:    */   public int setAddress(int addr)
/*  79:    */   {
/*  80: 96 */     byte[] bAddr = new byte[1];
/*  81: 97 */     bAddr[0] = ((byte)(addr & 0xFF));
/*  82:    */     
/*  83: 99 */     int ftStatus = I2C_Check(false);
/*  84:100 */     if (ftStatus != 0) {
/*  85:101 */       return ftStatus;
/*  86:    */     }
/*  87:104 */     ftStatus = cmdSet(92, bAddr[0]);
/*  88:106 */     if (ftStatus < 0) {
/*  89:107 */       return 18;
/*  90:    */     }
/*  91:109 */     return 0;
/*  92:    */   }
/*  93:    */   
/*  94:    */   public int read(byte[] buffer, int sizeToTransfer, int[] sizeTransferred)
/*  95:    */   {
/*  96:116 */     int[] maxSize = new int[1];
/*  97:117 */     long startTime = System.currentTimeMillis();
/*  98:118 */     int iTimeout = this.mFtDev.getReadTimeout();
/*  99:120 */     if (sizeToTransfer < 1) {
/* 100:121 */       return 6;
/* 101:    */     }
/* 102:124 */     int ftStatus = I2C_Check(false);
/* 103:125 */     if (ftStatus != 0) {
/* 104:126 */       return ftStatus;
/* 105:    */     }
/* 106:129 */     ftStatus = getMaxTransferSize(maxSize);
/* 107:130 */     if (ftStatus != 0) {
/* 108:131 */       return ftStatus;
/* 109:    */     }
/* 110:134 */     if (sizeToTransfer > maxSize[0]) {
/* 111:135 */       return 1010;
/* 112:    */     }
/* 113:138 */     sizeTransferred[0] = 0;
/* 114:139 */     int dataSize = this.mFtDev.getQueueStatus();
/* 115:141 */     while ((dataSize < sizeToTransfer) && (System.currentTimeMillis() - startTime < iTimeout)) {
/* 116:142 */       dataSize = this.mFtDev.getQueueStatus();
/* 117:    */     }
/* 118:145 */     if (dataSize > sizeToTransfer) {
/* 119:146 */       dataSize = sizeToTransfer;
/* 120:    */     }
/* 121:149 */     ftStatus = this.mFtDev.read(buffer, dataSize);
/* 122:151 */     if (ftStatus < 0) {
/* 123:152 */       return 1011;
/* 124:    */     }
/* 125:155 */     sizeTransferred[0] = ftStatus;
/* 126:    */     
/* 127:157 */     return 0;
/* 128:    */   }
/* 129:    */   
/* 130:    */   public int write(byte[] buffer, int sizeToTransfer, int[] sizeTransferred)
/* 131:    */   {
/* 132:163 */     int[] maxSize = new int[1];
/* 133:165 */     if (sizeToTransfer < 1) {
/* 134:166 */       return 6;
/* 135:    */     }
/* 136:169 */     int ftStatus = I2C_Check(false);
/* 137:170 */     if (ftStatus != 0) {
/* 138:171 */       return ftStatus;
/* 139:    */     }
/* 140:174 */     ftStatus = getMaxTransferSize(maxSize);
/* 141:175 */     if (ftStatus != 0) {
/* 142:176 */       return ftStatus;
/* 143:    */     }
/* 144:179 */     if (sizeToTransfer > maxSize[0]) {
/* 145:180 */       return 1010;
/* 146:    */     }
/* 147:183 */     sizeTransferred[0] = 0;
/* 148:    */     
/* 149:185 */     ftStatus = this.mFtDev.write(buffer, sizeToTransfer);
/* 150:    */     
/* 151:187 */     sizeTransferred[0] = ftStatus;
/* 152:189 */     if (sizeToTransfer == ftStatus) {
/* 153:190 */       return 0;
/* 154:    */     }
/* 155:192 */     return 10;
/* 156:    */   }
/* 157:    */   
/* 158:    */   boolean I2C_ModeCheck()
/* 159:    */   {
/* 160:201 */     if ((this.mFt4222Dev.mChipStatus.chip_mode == 0) || (this.mFt4222Dev.mChipStatus.chip_mode == 3)) {
/* 161:202 */       return true;
/* 162:    */     }
/* 163:205 */     return false;
/* 164:    */   }
/* 165:    */   
/* 166:    */   int I2C_Check(boolean isMaster)
/* 167:    */   {
/* 168:210 */     if (isMaster)
/* 169:    */     {
/* 170:211 */       if (this.mFt4222Dev.mChipStatus.function != 1) {
/* 171:212 */         return 1004;
/* 172:    */       }
/* 173:    */     }
/* 174:215 */     else if (this.mFt4222Dev.mChipStatus.function != 2) {
/* 175:216 */       return 1004;
/* 176:    */     }
/* 177:220 */     return 0;
/* 178:    */   }
/* 179:    */   
/* 180:    */   int getMaxTransferSize(int[] pMaxSize)
/* 181:    */   {
/* 182:228 */     pMaxSize[0] = 0;
/* 183:    */     
/* 184:230 */     int maxBuckSize = this.mFt4222Dev.getMaxBuckSize();
/* 185:232 */     switch (this.mFt4222Dev.mChipStatus.function)
/* 186:    */     {
/* 187:    */     case 2: 
/* 188:235 */       pMaxSize[0] = (maxBuckSize - 4);
/* 189:    */       
/* 190:    */ 
/* 191:    */ 
/* 192:    */ 
/* 193:    */ 
/* 194:    */ 
/* 195:    */ 
/* 196:243 */       break;
/* 197:    */     default: 
/* 198:245 */       return 17;
/* 199:    */     }
/* 200:248 */     return 0;
/* 201:    */   }
/* 202:    */ }



/* Location:           E:\code\demo\A108DevForBankComm\libs\d2xx.jar

 * Qualified Name:     com.ftdi.j2xx.ft4222.FT_4222_I2c_Slave

 * JD-Core Version:    0.7.0.1

 */